package com.example.login

import android.os.Bundle
import android.text.Editable
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.path.RouterPath
import com.example.common.view.view.BaseMvvmActivity
import com.example.login.databinding.ActivityLoginBinding
import com.example.login.vm.LoginViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path= RouterPath.PATH_LOGIN_PAGE)
class LoginActivity : BaseMvvmActivity<ActivityLoginBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_login
    val loginViewModel:LoginViewModel by viewModels ()
    override fun initView() {
        super.initView()
        //点击跳转注册页面
        mDataBinding.gotoRegister.setOnClickListener {
            ARouter.getInstance().build("/login/RegisterActivity").navigation()
        }
        mDataBinding.btnLogin.setOnClickListener {
            loginViewModel.postLogin(mDataBinding.etPass.text.toString(),mDataBinding.etUser.text.toString())
        }
        loginViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("登录成功")
            it.data.forEach {
                MMKV.defaultMMKV().encode("token",it.token)
            }
            ARouter.getInstance().build("/main/MainActivity").navigation()
            finish()
        }
        loginViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }

}